<?php
namespace Admin\Controller;
use Think\Controller;
class VideoController extends Controller
{
    public function index()
    {
        $this->display();
       
    }

    /*视频信息*/
    public function videolist(){
        $video = M('video'); //实例化模型类
        $videolist = $video->order("vid desc")->select();

        $this->assign('videolist',$videolist);
        $this->display();
    }

 /*视频信息(超级管理员查看)*/
    public function Svideolist(){
        $video = M('video');
        $Svideolist = $video->order("vid desc")->select();

       

        $this->assign('Svideolist',$Svideolist);
        $this->display();
    }
    

    /*添加视频*/
   public function addvideo()
{
       
       $this->display();
            }




    public function doaddvideo(){
        $data=$_POST;
        $file=$_FILES;
        $videoModel=M('video');
        $createresult=$videoModel->create(); // 根据表单提交的POST数据创建数据对象
        if($createresult){
          // $videoModel->videoname=trim(I('post.videoname'));  //获取post参数 ，输入变量 $_POST['videoname']
          // // $videoModel->vid=trim(I('post.vid'));            
          // $videoModel->videoaddress=trim(I('post.videoaddress'));
          // $videoModel->videointro=trim(I('post.videointro'));
          
            if($file['videoaddress']['error']==0){
                  
                $info=$this->Upload();
                $videoModel->videoaddress=$info['videoaddress']['savename'];

            }else{
                $videoModel->videoaddress="30.mp4"; 
                
                
     }
      $addresult=$videoModel->add();

      if (false !== $addresult)
                 {
                    $this->success('添加成功！', '../Video/videolist');
                }else{ 
                   $this->error('添加失败!'); 
               }


     } else{
            $this->error($videoModel->getError());
    }
    
}

public function editvideo(){
      
        $vid=$_GET['vid'];
        $videoModel=M('video');
        $video = $videoModel->where("vid=$vid")->select();
        $this->video=$video;     //name属性对应的值 $this->对应值=$find;
        $this->display();

    }
    public function doupdate(){
            $data=$_POST;
            $file=$_FILES;
            //dump($file);
            $vid=$data['vid'];  //获取vid
            //dump($id);        
            $videoModel = M('video'); // 实例化Video模型
            // 要修改的数据对象属性赋值
            //$userModel->create();   // 根据表单提交的POST数据创建数据对象
            //dump($data);
        //$result=$userModel->where("id=$id")->save($data);

        if ($file['videoaddress']['size']==0) {
            //没有修改头像
            $result=$videoModel->where("vid=$vid")->save($data);
  
        }else{
            //修改头像
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize  = 31457280000000000000000000000000000000000000000 ;// 设置附件上传大小
            $upload->allowExts  = array('mp4', 'rmvb', 'mkv', 'f4v','avi','3gp','swf','mpeg','rm');// 设置附件上传类型
            $upload->rootPath  =  './';
            $upload->savePath =  'Public/Uploads/photo/';// 设置附件上传目录
            $upload->autoSub = false;
            $upload->saveName = date("YmdHis").rand(100,999);//定义名字
            $info = $upload->upload();
    
            if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
             }else{// 上传成功 获取上传文件信息
                 //保存表单数据 包括附件数据
                $data['videoaddress']= $info['videoaddress']['savename']; 
                $result=$videoModel->where("vid=$vid")->save($data);
                    if ($data['oldpic']!='nopic.jpg') {
                    $oldpicPath='Public/Uploads/photo/'.$data['oldpic'];
                    unlink($oldpicPath);
                        }
                    }
            }
        if($result){
             $this->success('修改成功！','../Video/videolist');
             }
            else{
                $this->error('修改失败！');
                }

    }
        public function Upload(){
                //上传头像
              $upload = new \Think\Upload();// 实例化上传类
              $upload->maxSize  = 31457280000 ;// 设置附件上传大小
              $upload->allowExts  = array('mp4', 'rmvb', 'mkv', 'f4v','avi','3gp','swf','mpeg','rm');// 设置附件上传类型
              // $upload->rootPath  =  './';
              $upload->rootPath = "./"; 
              $upload->savePath =  'Public/Uploads/photo/';// 设置附件上传目录
              $upload->autoSub = false;
              $upload->saveName = date("YmdHis").rand(100,999);//定义名字
               // 上传单个文件 
              $info = $upload->upload();                    
                  if(!$info) {// 上传错误提示错误信息
                      $this->error($upload->getError());
                    }else{// 上传成功 
                        return $info;
                    }
                } 
            
   
    /*删除视频*/

    public function delvideo($vid)
    {
  
        $video=M('video');
        $res=$video->where("vid=$vid")->find();
        $photoPath = realpath("./Public/Uploads/photo/".$res['videoaddress']);
        unlink($photoPath);
        //$res=$usersmodel->where("id=$id")->delete();
        if ($video->delete($vid)) {
            $this->success('删除成功！',U('videolist'));
        } else {
            $this->error('删除失败');
        }
    }
}